package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.entity.UserAuth;
import com.example.service.UserAuthService;
import com.example.service.UserService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "评审是否为贫困用户") //swagger的注释
@CrossOrigin
@RequestMapping("/userAuth")
@RestController
public class UserAuthController {


    @Autowired
    private UserAuthService userAuthService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "分页查询用户申请信息")
    @GetMapping("selectUserAuth/{current}/{limit}")
    public R selectUser(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        QueryWrapper<UserAuth> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<UserAuth> userPage = new Page<>(current, limit);
        userAuthService.page(userPage, wrapper);
        //获取分页后数据
        long total = userPage.getTotal();
        List<UserAuth> list = userPage.getRecords();
        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "分页带条件查询用户申请信息")
    @PostMapping("selectUserAuth/{current}/{limit}")
    public R searchUser(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit,
            @RequestBody(required = false) UserAuth userAuth
    ) {
        QueryWrapper<UserAuth> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(userAuth)) {
            if (!StringUtils.isEmpty(userAuth.getUserId())) {
                wrapper.eq("user_id", userAuth.getUserId());
            }
            if (!StringUtils.isEmpty(userAuth.getIsPoor())) {
                wrapper.eq("is_poor",userAuth.getIsPoor());
            }
        }
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<UserAuth> userPage = new Page<>(current, limit);
        userAuthService.page(userPage, wrapper);
        //获取分页后数据
        long total = userPage.getTotal();
        List<UserAuth> list = userPage.getRecords();
        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "添加/提交贫困审核")
    @PostMapping("createUserAuth")
    public R addUserAuth(@RequestBody UserAuth userAuth) {
        boolean b = userAuthService.save(userAuth);
        if (b) {
            return R.ok();
        } else {
            return R.error().message("创建失败！");
        }
    }

    @ApiOperation(value = "审核贫困审核通过")
    @PostMapping("updateUserAuth")
    public R updateUserAuth(@RequestBody UserAuth userAuth) {
        boolean b = userAuthService.updateById(userAuth);
        User user = new User();
        user.setUserId(userAuth.getUserId());
        user.setLevel(2);
        boolean bb = userService.updateById(user);
        if (b && bb) {
            return R.ok();
        } else {
            return R.error().message("申请出错！");
        }
    }

    @ApiOperation(value = "审核贫困审核不通过")
    @PostMapping("updateNoUserAuth")
    public R updateNpUserAuth(@RequestBody UserAuth userAuth) {
        boolean b = userAuthService.updateById(userAuth);
        if (b) {
            return R.ok();
        } else {
            return R.error().message("申请出错！");
        }
    }


}
